package SessionBeans;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import DtoFactory.FactoryDTO;
import Entities.FacturaBean;
import Entities.ItemFacturaBean;
import Entities.RodamientoBean;
import EntitiesDTO.FacturaDTO;

public @Stateless class AdminFacturacionBean implements AdminFacturacion {

	@PersistenceContext
	private EntityManager em;
	private FactoryDTO dtofactory;

	public AdminFacturacionBean() {
		dtofactory = FactoryDTO.getInstance();

	}
	
	@Override
	public void saveFacturacion(FacturaDTO fact) {
		FacturaBean factura = dtofactory.inflacteFacturacion(fact);
		for (ItemFacturaBean item : factura.getItemsFactura()) {
			RodamientoBean rodam = (RodamientoBean) em.createQuery(
					"select r from RodamientoBean r where r.codigo=?1")
					.setParameter(1, item.getRodamiento().getCodigo())
					.getSingleResult();
			item.setRodamiento(rodam);
		}
		em.persist(factura);
	}

}
